{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e0ab4a60-bc68-446d-ae13-6bd90d54ae44",
   "metadata": {},
   "outputs": [],
   "source": [
    "# imports\n",
    "import os\n",
    "from dotenv import load_dotenv\n",
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "from IPython.display import Markdown, display\n",
    "from openai import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "749afaa0-a82e-4783-91fc-f69756075606",
   "metadata": {},
   "outputs": [],
   "source": [
    "# A class to represent a Webpage\n",
    "\n",
    "# Some websites need you to use proper headers when fetching them:\n",
    "headers = {\n",
    " \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36\"\n",
    "}\n",
    "\n",
    "class Website:\n",
    "\n",
    "    def __init__(self, url):\n",
    "        \"\"\"\n",
    "        Create this Website object from the given url using the BeautifulSoup library\n",
    "        \"\"\"\n",
    "        self.url = url\n",
    "        response = requests.get(url, headers=headers)\n",
    "        soup = BeautifulSoup(response.content, 'html.parser')\n",
    "        self.title = soup.title.string if soup.title else \"No title found\"\n",
    "        for irrelevant in soup.body([\"script\", \"style\", \"img\", \"input\"]):\n",
    "            irrelevant.decompose()\n",
    "        self.text = soup.body.get_text(separator=\"\\n\", strip=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7e760d9c-d899-49e5-8b8f-c202794486cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "API key found and looks good so far!\n"
     ]
    }
   ],
   "source": [
    "# Load environment variables in a file called .env\n",
    "\n",
    "load_dotenv(override=True)\n",
    "api_key = os.getenv('OPENAI_API_KEY')\n",
    "\n",
    "# Check the key\n",
    "\n",
    "if not api_key:\n",
    "    print(\"No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!\")\n",
    "elif not api_key.startswith(\"sk-proj-\"):\n",
    "    print(\"An API key was found, but it doesn't start sk-proj-; please check you're using the right key - see troubleshooting notebook\")\n",
    "elif api_key.strip() != api_key:\n",
    "    print(\"An API key was found, but it looks like it might have space or tab characters at the start or end - please remove them - see troubleshooting notebook\")\n",
    "else:\n",
    "    print(\"API key found and looks good so far!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8efb8bb3-9be9-404b-aff5-306db64a75e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "openai = OpenAI()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cf677c78-012c-4b86-a76c-be47ed3cb987",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define our system prompt - you can experiment with this later, changing the last sentence to 'Respond in markdown in Spanish.\"\n",
    "\n",
    "system_prompt = \"You are an assistant that analyzes the \\\n",
    "the dashboard in a website and provides a short executive summary, ignoring text that might be navigation related. \\\n",
    "Respond in markdown.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "970a493a-880a-4206-9609-eee0651aa91f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# A function that writes a User Prompt that asks for summaries of websites:\n",
    "\n",
    "def user_prompt_for(website):\n",
    "    user_prompt = f\"You are looking at a website titled {website.title}\"\n",
    "    user_prompt += \"\\nPlease provide a detailed summary of the report for the year in markdown for its user (CFO); \\\n",
    "The summary should be in a suitable form which could be sent through a mail for the exective.\\n\\n\"\n",
    "    user_prompt += website.text\n",
    "    return user_prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2520cdd1-4755-4c87-854f-430e81dbc3fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "def messages_for(website):\n",
    "    return [\n",
    "        {\"role\": \"system\", \"content\": system_prompt},\n",
    "        {\"role\": \"user\", \"content\": user_prompt_for(website)}\n",
    "    ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7452990b-352b-43cc-adc6-4307d6d5c1d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def summarize(url):\n",
    "    website = Website(url)\n",
    "    response = openai.chat.completions.create(\n",
    "        model = \"gpt-4o-mini\",\n",
    "        messages = messages_for(website)\n",
    "    )\n",
    "    return response.choices[0].message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7372da3c-f3c7-455b-825e-f54d3b0cee68",
   "metadata": {},
   "outputs": [],
   "source": [
    "def display_summary(url):\n",
    "    summary = summarize(url)\n",
    "    display(Markdown(summary))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "be7d57dc-bec1-4771-9d15-d80fd4d3fbb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "# Executive Summary: Revenue & Profitability Dashboard\n",
       "\n",
       "**To:** [CFO Name]  \n",
       "**From:** [Your Name]  \n",
       "**Date:** [Current Date]  \n",
       "**Subject:** Yearly Analysis of Revenue & Profitability  \n",
       "\n",
       "---\n",
       "\n",
       "Dear [CFO Name],\n",
       "\n",
       "I am pleased to present the Year-over-Year analysis derived from the Revenue & Profitability Dashboard. This dashboard has been designed to provide concise insights into our core financial performance metrics, enabling data-driven decision-making at the executive level.\n",
       "\n",
       "### Key Metrics Overview:\n",
       "- **Revenue**: Comprehensive insights into total revenue across various regions and product categories, indicating sustainable growth patterns.\n",
       "- **Profit**: Detailed profitability analysis segmented by customer groups, revealing key opportunities for margin improvement and cost optimization.\n",
       "- **Unit Sales**: Analysis of unit sales trends that highlight product performance and demand fluctuations.\n",
       "\n",
       "### Insights by Segment:\n",
       "- **Regional Performance**: Comparative analysis of revenue and profitability by region helps identify areas of growth and those requiring strategic intervention.\n",
       "- **Product Performance**: A focused review of individual product lines shows which offerings are driving profitability and where we might consider realignment or innovation.\n",
       "\n",
       "### Dashboard Features:\n",
       "- A **clean and focused layout** reduces cognitive load, allowing for quick assimilation and understanding of critical data points.\n",
       "- **Contextual metrics** that align with our overarching business strategy, ensuring that our analysis supports our organizational goals.\n",
       "- **Clear comparison points** are established to aid executives in making informed and timely decisions.\n",
       "- Insightful details are presented at both product and regional levels, facilitating targeted strategies for improvement.\n",
       "\n",
       "### Conclusion:\n",
       "The integration of design and context in our dashboard framework turns our data into strategic tools, empowering us to make faster and more informed decisions that drive real business impact.\n",
       "\n",
       "Please feel free to reach out for a more detailed discussion or specific metrics that may interest you.\n",
       "\n",
       "Best regards,\n",
       "\n",
       "[Your Name]  \n",
       "[Your Position]\n",
       "\n",
       "--- \n",
       "\n",
       "*Note: For additional inquiries or insights, feel free to follow our updates on LinkedIn or contact me directly.*"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display_summary(\"https://community.fabric.microsoft.com/t5/Data-Stories-Gallery/Revenue-amp-Profitability-Dashboard/td-p/4780272\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1aa33cfd-d497-4ab8-abb5-eb4e6030890b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
